local qufu = fk.CreateSkill{
  name = "hanqing__qufu",
}

Fk:loadTranslationTable{
  ["hanqing__qufu"] = "趋附",
  [":hanqing__qufu"] = "当你受到伤害后，你可以令至多两名其他角色各展示(1)张牌，然后你获得其中一张。若你因此获得【杀】，你摸一张牌。",

  ["#hanqing__qufu-choose"] = "趋附：令至多两名角色各展示%arg张牌，你获得其中一张",
  ["#hanqing__qufu-show"] = "趋附：请展示%arg张手牌，%src 可以获得其中一张",
  ["#hanqing__qufu-prey"] = "趋附：获得其中一张牌，若为【杀】你再摸一张牌",

  ["$hanqing__qufu1"] = "群山尚随东岳，可知顺应人主乃自然之理。",
  ["$hanqing__qufu2"] = "外示贞刚，内存奉承；美官丰禄，自入我怀。",
}

qufu:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(qufu.name) and
      table.find(player.room:getOtherPlayers(player, false), function(p)
        return not p:isKongcheng()
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function(p)
      return not p:isKongcheng()
    end)
    local n = player:hasSkill("hanqing__fusheng") and player.kingdom == "chu" and 2 or 1
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 2,
      targets = targets,
      skill_name = qufu.name,
      prompt = "#hanqing__qufu-choose:::"..n,
      cancelable = true,
    })
    if #tos > 0 then
      room:sortByAction(tos)
      event:setCostData(self, {tos = tos, extra_data = n})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    local n = event:getCostData(self).extra_data
    local dat = {}
    for _, p in ipairs(tos) do
      if not p.dead and not p:isKongcheng() then
        local cards = p:getCardIds("h")
        if #cards > n then
          cards = room:askToCards(p, {
          min_num = n,
          max_num = n,
          include_equip = false,
          skill_name = qufu.name,
          prompt = "#hanqing__qufu-show:"..player.id.."::"..n,
          cancelable = false,
        })
        end
        dat[p] = cards
        p:showCards(cards)
      end
    end
    if player.dead then return end
    local card_data = {}
    for p, ids in pairs(dat) do
      ids = table.filter(ids, function(id)
        return table.contains(p:getCardIds("h"), id)
      end)
      if #ids > 0 then
        table.insert(card_data, { p.general, ids })
      end
    end
    local card = room:askToChooseCard(player, {
      target = target,
      flag = { card_data = card_data },
      skill_name = qufu.name,
      prompt = "#hanqing__qufu-prey",
    })
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, qufu.name, nil, true, player)
    if player.dead then return end
    if Fk:getCardById(card).trueName == "slash" then
      player:drawCards(1, qufu.name)
    end
  end,
})

return qufu
